package com.clearance.mine_clearance.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.clearance.mine_clearance.bot.MineClearanceBot;
import com.clearance.mine_clearance.entry.BetInfo;
import com.clearance.mine_clearance.entry.Config;
import com.clearance.mine_clearance.entry.RedPacket;
import com.clearance.mine_clearance.entry.TgUser;
import com.clearance.mine_clearance.mapper.BetInfoMapper;
import com.clearance.mine_clearance.mapper.RedPacketMapper;
import com.clearance.mine_clearance.mapper.TgUserNapper;
import com.clearance.mine_clearance.untils.RedUntils;
import com.clearance.mine_clearance.untils.RedisUntils;
import com.clearance.mine_clearance.config.Constants;
import com.clearance.mine_clearance.untils.SendMessageUntils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.api.methods.AnswerCallbackQuery;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


@Component
public class RedWarsTask {


    @Autowired
    private TgUserNapper tgUserNapper;


    @Autowired
    private RedUntils redUntils;

    @Autowired
    private ExecutorService executorService;

    @Scheduled(cron = "0/3 * * * * ?")
    public void startTask() {
        Config config = RedisUntils.getConfig();
        Integer isOpenAutoSnatch = config.getIsOpenAutoSnatch();
        if (isOpenAutoSnatch == 1) {
            return;
        }
        LambdaQueryWrapper<TgUser> tgUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tgUserLambdaQueryWrapper.eq(TgUser::getType, 1);
        List<TgUser> tgUserList = tgUserNapper.selectList(tgUserLambdaQueryWrapper);
        List<String> listKeyValue = RedisUntils.getListKeyValue(Constants.redPacket);
        executorService.submit(() -> {
            for (String key : listKeyValue) {
                int index = (int) (Math.random() * tgUserList.size());
                TgUser tgUser = tgUserList.get(index);
                RedPacket redPacket = JSON.parseObject(key, RedPacket.class);
                redUntils.addIndex(redPacket.getId(), tgUser, null);

            }
        });
    }

    //@Scheduled(cron = "0 0/1 * * * ? ")
    public void giveRedEnvelopesTask() {
        Config config = RedisUntils.getConfig();
        if (config.getAutoSend() == 1) {
            return;
        }
        LambdaQueryWrapper<TgUser> tgUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tgUserLambdaQueryWrapper.eq(TgUser::getGivePayType, 1);
        List<TgUser> tgUserList = tgUserNapper.selectList(tgUserLambdaQueryWrapper);
        if (tgUserList == null || tgUserList.size() == 0) {
            return;
        }
        int index = (int) (Math.random() * tgUserList.size());
        TgUser tgUser = tgUserList.get(index);
        Random random = new Random();
        double randomValue = random.nextDouble() * (config.getMaxRedPacket().subtract(config.getMaxRedPacket()).doubleValue()) + config.getMinRedPacket().doubleValue();
        RedPacket redPacket = new RedPacket();
        redPacket.setTgUserId(tgUser.getId());
        redPacket.setAmount(BigDecimal.valueOf(randomValue));
        redPacket.setMulriple(config.getMulriple());
        redPacket.setRob(0);
        redPacket.setRedPacketSum(config.getRedPacketSum());
        redPacket.setTime(new Date());
        redPacket.setMines(random.nextInt(10));
        redUntils.giveRedEnvelopesTask(tgUser, redPacket, null, false);
    }
}




























